package com.cenntro.common.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public class BigDecimalUtil {

	
	
	/**
	 * 元转分
	 * @param yuan
	 * @return
	 */
	public static Integer yuan2Cent(Object yuan){
		if (yuan == null || StringUtils.isBlank(yuan.toString())){return 0;}
		BigDecimal y = new BigDecimal(yuan.toString());
		return y.multiply(BigDecimal.valueOf(100)).intValue();
	}
	
	/**
	 * 分转元 默认保留2个小数且4舍5入
	 * @param cent
	 * @return
	 */
	public static String cent2Yuan(Object cent){
		if (cent == null || org.apache.commons.lang3.StringUtils.isBlank(cent.toString())){ return "0.00";}
		BigDecimal c = new BigDecimal(cent.toString());
		
		return c.divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
	}
	
	public static String divide(Object arg1, Object arg2, Integer scale){
		if (arg2 == null || arg1 == null || org.apache.commons.lang3.StringUtils.isBlank(arg1.toString())){ return "";}
		BigDecimal c1= new BigDecimal(arg1.toString());
		BigDecimal c2 = new BigDecimal(arg2.toString());
		return c1.divide(c2, scale == null ? 2: scale,  BigDecimal.ROUND_HALF_UP).toString();
	}
	
	public static BigDecimal subtract(Object arg1, Object arg2){
		return getBigDecimal(arg1).subtract(getBigDecimal(arg2));
	}
	
	/**
	 * 相加
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * 
	 * @return
	 */
	public static BigDecimal add(Object arg1, Object arg2, Object... arg3){
		BigDecimal result = getBigDecimal(arg1).add(getBigDecimal(arg2));
		if (ArrayUtils.isNotEmpty(arg3)){
			for (Object arg : arg3){
				result = BigDecimalUtil.add(result, arg);
			}
		}
		return result;
	}
	/**
	 * 相乘
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 */
	public static BigDecimal multi(Object arg1, Object arg2, Object... arg3){
		BigDecimal result = getBigDecimal(arg1).multiply(getBigDecimal(arg2));
		if (ArrayUtils.isNotEmpty(arg3)){
			for (Object arg : arg3){
				result = BigDecimalUtil.multi(result, arg).setScale(2, RoundingMode.HALF_UP);
			}
		}
		return result;
	}
	
	/**
	 * 相乘 车辆系统上报数据特制
	 * arg1 下于0 返回
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 */
	public static BigDecimal multi4Vehicle(Object arg1, Object arg2, Object... arg3){
		if(NumUtil.toInt(arg1) < 0){
			return getBigDecimal(arg1);
		}
		BigDecimal result = getBigDecimal(arg1).multiply(getBigDecimal(arg2));
		if (ArrayUtils.isNotEmpty(arg3)){
			for (Object arg : arg3){
				result = BigDecimalUtil.multi(result, arg);
			}
		}
		return result;
	}
	
	/**
	 * 设置
	 * @param bg
	 * @param scale 保留位数
	 * @param roundingMode 模式
	 * @return
	 */
	public static BigDecimal setscale(BigDecimal bg, int scale, int roundingMode){
		if  (bg == null){
			return BigDecimal.valueOf(0); 
		}
		return bg.setScale(scale, roundingMode);
	}
	
	public static BigDecimal getBigDecimal(Object arg){
		if (arg == null){
			return BigDecimal.valueOf(0);
		}
		return new BigDecimal(arg.toString());
	}
	
	public static void main(String[] args) {
		System.out.println(yuan2Cent(1.12051111));
		
		System.out.println(yuan2Cent(0.1205109081));
		
		System.out.println(yuan2Cent(0.1205112));
		
		System.out.println(cent2Yuan(122.255));
		System.out.println(cent2Yuan(122.316));
		System.out.println(cent2Yuan(122.417));
		System.out.println(cent2Yuan(122.518));
		
		System.out.println(setscale(new BigDecimal("122.8"), 2, BigDecimal.ROUND_HALF_UP));
		
		System.out.println(multi("00", 0.1,2));
		System.out.println(divide(37.0001, 60,6));
		System.out.println(add("37.0001", "60",6));
	}
}
